﻿<!DOCTYPE html>
<html>
<head>
    <script src="/js/header.js"></script>
    <script src="/lib/date/wdatepicker.js"></script>
    <link href="/lib/select2/select2.min.css" rel="stylesheet" />
    <script src="/lib/select2/select2.min.js"></script>
</head>
<body class="bg_white">
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form" onsubmit="return false;">
            <div class="tab-content">
                <div class="tab-pane fade in active">
                    <input type="hidden" name="paymentInterfaceId" />
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>公司名称：</label>
                        <div class="wd300 m15_l">
                            <select class="form-control wd300" name="companyId">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>接口类型：</label>
                        <div class="wd300 m15_l">
                            <select class="form-control wd300" name="paymentType">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label">商户名称(号)：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="companyName" maxlength="500" autocomplete="off" placeholder="商户名称(号)只能由中文、字母、数字和下划线组成" />
                        </div>
                    </div>

                    <div class="form-group hide secretKey" id="secretKey">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>秘钥：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="secretKey" autocomplete="off" placeholder="秘钥只能为字母或数字组成" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>付款区间起：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="paymentStart" maxlength="16" autocomplete="off" placeholder="请输入大于0的金额，最多保留四位小数" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>付款区间迄：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="paymentEnd" maxlength="16" autocomplete="off" placeholder="请输入大于0的金额，最多保留四位小数" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">客户收款银行：</label>
                        <div class="wd300 m15_l">
                            <select class="form-control wd300 js-example-basic-multiple" name="withdrawalBank" multiple="multiple"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">客户排除银行：</label>
                        <div class="wd300 m15_l">
                            <select class="form-control wd300 js-example-basic-multiple" name="excludedWithdrawalBank" multiple="multiple"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>存款等级：</label>
                        <div class="wd300 m15_l">
                            <input type="checkbox" name="depositType" value="A" />A
                            <input type="checkbox" name="depositType" value="B" />B
                            <input type="checkbox" name="depositType" value="C" />C
                            <input type="checkbox" name="depositType" value="D" />D
                            <input type="checkbox" name="depositType" value="E" />E
                            <input type="checkbox" name="depositType" value="F" />F
                            <input type="checkbox" name="depositType" value="G" />G
                            <input type="checkbox" name="depositType" value="H" />H
                            <input type="checkbox" name="depositType" value="I" />I
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>订单提交笔数上线：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="paymentMax" autocomplete="off" placeholder="订单提交笔数由数字组成且最大为99" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label">排序：</label>
                        <div class="wd300 m15_l">
                            <input class="form-control" type="text" name="sortNo" autocomplete="off" maxlength="2" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>每天执行：</label>
                        <div class="wd300 m15_l">
                            <label class="m10_r">
                                <input type="radio" name="limitRepeat" value="2" checked="checked" /> 禁用
                            </label>
                            <label>
                                <input type="radio" name="limitRepeat" value="1" /> 启用
                            </label>
                        </div>
                    </div>

                    <div class="form-group hide" id="limitOpenDate">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>开启时间：</label>
                        <div class="wd300 m15_l">
                            <input id="iptOpenDate" class="form-control" type="text" name="limitOpenDate" autocomplete="off" onfocus="WdatePicker({dateFmt:'HH:mm'})" />
                        </div>
                    </div>
                    <div class="form-group hide" id="limitCloseDate">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>关闭时间：</label>
                        <div class="wd300 m15_l">
                            <input id="iptCloseDate" class="form-control" type="text" name="limitCloseDate" autocomplete="off" onfocus="WdatePicker({dateFmt:'HH:mm'})" />
                        </div>
                    </div>

                </div>
            </div>
        </form>
    </section>
    <script type="text/javascript">
    var id = 0;
    var company = $("select[name='companyId']");
    var paymentType = $("select[name='paymentType']");
    var withdrawalBank = $("select[name='withdrawalBank']");
    var excludedWithdrawalBank = $("select[name='excludedWithdrawalBank']");
    var depositType = $("select[name='depositType']");

    $(function () {
        id = FlashPay.Util.GetQueryString("id");

        withdrawalBank.select2({
            placeholder: '请选择银行'
        });

        excludedWithdrawalBank.select2({
            placeholder: '请选择银行'
        });

        init(id);

        $("input[name='limitRepeat']").click(function () {
            if ($(this).val() == 1) {
                $("#limitOpenDate").removeClass("hide");
                $("#limitCloseDate").removeClass("hide");
            } else {
                $("#limitOpenDate").addClass("hide");
                $("#limitCloseDate").addClass("hide");
                FlashPay.Util.SetInputVal("limitCloseDate", "");
                FlashPay.Util.SetInputVal("limitOpenDate", "");
            }
        });
    });


    function init(id) {

        var cover = FlashPay.UI.Mask({
            obj: $("body"),
            opacity: 0.5
        });


        FlashPay.Util.Ajax({
            type: "POST",
            url: "/PaymentInterface/GetPaymentInterface/" + id,
            data: null,
            success: function (res) {
                if (res.success) {

                    
                    FlashPay.Util.SetInputVal("paymentInterfaceId", res.data.paymentInterfaceID);

                    if (res.data.companyResponse != null) {
                        $.each(res.data.companyResponse, function (i, item) {
                            company.append("<option value='" + item.companyID + "'>" + item.companyName + "</option >");
                        });

                        company.find("option[value=" + res.data.companyID + "]").prop("selected", true);
                    }

                    if (res.data.sysConfigTypeList != null) {
                        $.each(res.data.sysConfigTypeList, function (i, item) {
                            paymentType.append("<option value='" + item.configValue + "'>" + item.configContent + "</option >");
                        }); 

                        paymentType.find("option[value=" + res.data.paymentType + "]").prop("selected", true);
                    }

                    FlashPay.Util.SetInputVal("companyName", res.data.companyName);

                    FlashPay.Util.SetInputVal("secretKey", res.data.secretKey);

                    if (res.data.paymentInterfaceID > 0) {
                        if (res.data.secretKeyPermission == "PaymentInterface0005") {
                            $("div.secretKey").removeClass("hide");
                        } else {
                            $("div.secretKey").remove();
                        }
                    }
                    else {
                        $("div.secretKey").removeClass("hide");
                    }

                    FlashPay.Util.SetInputVal("paymentStart", res.data.paymentStart);

                    FlashPay.Util.SetInputVal("paymentEnd", res.data.paymentEnd);
                    
                    if (res.data.bankInfoResponse != null) {
                        $.each(res.data.bankInfoResponse, function (i, item) {
                            withdrawalBank.append("<option value='" + item.bankCode + "'>" + item.bankName + "</option >");
                        }); 

                        $.each(res.data.bankInfoResponse, function (i, item) {
                            excludedWithdrawalBank.append("<option value='" + item.bankCode + "'>" + item.bankName + "</option >");
                        }); 
                    }

                    if (!FlashPay.Util.isNullOrEmptySpance(res.data.withdrawalBank) && res.data.paymentInterfaceID>0) {
                        let arr = res.data.withdrawalBank.split(",");
                        withdrawalBank.val(arr).trigger("change");
                    }
                    if (!FlashPay.Util.isNullOrEmptySpance(res.data.excludedWithdrawalBank)) {
                        let arr = res.data.excludedWithdrawalBank.split(",");
                        excludedWithdrawalBank.val(arr).trigger("change");
                    }
                    

                    if (!FlashPay.Util.isNullOrEmptySpance(res.data.depositType)) {
                        let arrDepositType = $.trim(res.data.depositType).split(",");
                        $.each(arrDepositType, function (i, item) {
                            $("input[name='depositType'][value=" + $.trim(item) + "]").attr("checked", true);
                        })
                    }

                    FlashPay.Util.SetInputVal("paymentMax", res.data.paymentMax);
                    FlashPay.Util.SetInputVal("sortNo", res.data.sortNo>0?res.data.sortNo:"");

                    $("input[name='limitRepeat'][value='" + res.data.limitRepeat + "']").attr("checked", true);

                    if (res.data.paymentInterfaceID > 0) {
                        company.attr("disabled", true);
                        paymentType.attr("disabled", true);
                        if (res.data.limitRepeat == 1) {
                            FlashPay.Util.SetInputVal("limitOpenDate", res.data.limitOpenDate);
                            FlashPay.Util.SetInputVal("limitCloseDate", res.data.limitCloseDate);
                            $("#limitOpenDate").removeClass("hide");
                            $("#limitCloseDate").removeClass("hide");
                        }
                    }
                }
                cover.Remove();
            },
            error: function (e) {
                cover.Remove();
                FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
            }
        });
    }

    function save() {

        var data = $("#form").serializeObject();

        var cover = FlashPay.UI.Mask({
            obj: $("body"),
            opacity: 0.5
        });

        FlashPay.Util.Ajax({
            type: "POST",
            url: "/PaymentInterface/PaymentInterfaceAddOrEdit",
            data: data,
            success: function (response) {
                if (response.success) {
                    cover.Remove();
                    FlashPay.UI.DialogOpener().Tip_success("操作成功！");
                    FlashPay.UI.DialogOpener().doSearch();
                    FlashPay.UI.CloseDialog();
                } else {
                    cover.Remove();
                    FlashPay.UI.Tip_short_warning("操作失败！" + response.errorMessage);
                }
            },
            error: function (json) {
                cover.Remove();
                FlashPay.UI.Tip_warning("操作失败！请查看网络，请重试。");
            }
        });
    }

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

        //加载详细信息
        function showDetailInfo(id, paymentType) {
            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            FlashPay.Util.Ajax({
                type: "GET",
                url: "/SysConfig/GetManyInfo",
                data: { CompanyId: id, PaymentType: paymentType },
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    cover.Remove();
                    if (data.success) {
                        let datas = data.data;
                        showCompanyList(datas.companyList); //加载公司下拉列表
                        showBankList(datas.bankList);       //加载银行卡下拉列表
                        showTypeList(datas.sysConfigTypeList);//加载接口类型列表

                        if (!FlashPay.Util.isNullOrEmptySpance(id)) {
                            let infos = datas.paymentInterface;

                            $("select[name='companyID']").find("option[value=" + infos.companyID + "]").prop("selected", true);
                            $("select[name='companyID']").attr("disabled", true);

                            $("select[name='paymentType']").find("option[value=" + infos.paymentType + "]").prop("selected", true);
                            $("select[name='paymentType']").attr("disabled", true);
                            $("#paymentTypeOld").val(infos.paymentType);

                            $("select[name='paymentMax']").find("option[value=" + infos.paymentMax + "]").prop("selected", true);
                            
                            
                            FlashPay.Util.SetInputVal("paymentStart", infos.paymentStart);
                            FlashPay.Util.SetInputVal("paymentEnd", infos.paymentEnd);

                            //存款等级
                            let depositIds = infos.depositType;
                            if (!FlashPay.Util.isNullOrEmptySpance(depositIds)) {
                                let dArr = $.trim(depositIds).split(",");
                                $.each(dArr, function (i, z) {
                                    $("input[name='depositType'][value=" + $.trim(z) + "]").attr("checked", true);
                                })
                            }

                            //客户银行
                            let bankIds = infos.withdrawalBank;
                            if (!FlashPay.Util.isNullOrEmptySpance(bankIds)) {
                                let bArr = bankIds.split(",");
                                $(".js-example-basic-multiple").val(bArr).trigger("change");
                            }

                            if (infos.limitRepeat == 1) {
                                $("#divOpenDate").removeClass("hidden");
                                $("#divCloseDate").removeClass("hidden");
                            }

                            //时间显示小时（数字）
                            if (!FlashPay.Util.isNullOrEmptySpance(infos.limitCloseDate)) {
                                let time = infos.limitCloseDate.replace("T", " ");
                                let hour = new Date(time).getHours();
                                if (hour < 10) hour = "0" + hour;
                                FlashPay.Util.SetInputVal("limitCloseDate", hour);
                            }

                            if (!FlashPay.Util.isNullOrEmptySpance(infos.limitOpenDate)) {
                                let time = infos.limitOpenDate.replace("T", " ");
                                let hour = new Date(time).getHours();
                                if (hour < 10) hour = "0" + hour;
                                FlashPay.Util.SetInputVal("limitOpenDate", hour);
                            }

                            FlashPay.Util.SetInputVal("companyName", infos.companyName);
                            FlashPay.Util.SetInputVal("secretKey", infos.secretKey);
                            $("input[name='limitRepeat']").get(infos.limitRepeat > 1 ? 0 : infos.limitRepeat).checked = true;
                        } else {
                            $("input[name='depositType'][value=\"A\"]").attr("checked", true);
                            $("input[name='paymentEnd']").val('');$("input[name='paymentStart']").val('');
                            $("input[name='limitCloseDate']").val('');$("input[name='limitOpenDate']").val('');
                            $("input[name='secretKey']").val('');$("input[name='companyName']").val('');
                        }

                        if (data.errorCode == "false") {
                            //$("input[name='secretKey']").val("******").attr("disabled", true);
                            $("#divSecretKey").remove();
                        }

                        $('.js-example-basic-multiple').select2({
                            placeholder: '请选择一家银行'
                        });

                    } else {
                        FlashPay.UI.Tip_short_warning(data.errorMessage == null ? "未找到内容" : data.errorMessage);
                        return;
                    }
                },
                error: function (json) {
                    cover.Remove();
                }
            });
        }

        //加载银行卡下拉列表
        function showBankList(bankInfoResponse) {
            if (bankInfoResponse != null) {
                $.each(bankInfoResponse, function (i, item) {
                    $("select[name='withdrawalBank']").append("<option value='" + item.bankCode + "'>" + item.bankName + "</option >");
                });
            }
        }

        //加载接口类型列表
        function showTypeList(tData) {
            if (tData != null) {
                $.each(tData, function (i, item) {
                    $("select[name='paymentType']").append("<option value='" + item.configValue + "'>" + item.configContent + "</option >");
                });
            }
        }

        //加载公司下拉列表
        function showCompanyList(result) {
            if (result != null) {
                $.each(result, function (i, item) {
                    $("select[name='companyID']").append("<option value='" + item.companyId + "'>" + item.companyName + "</option >");
                });
            }
        }

        //设置开启/关闭时间div显示
        function setDateShow() {
            $("input[name='limitRepeat']").click(function () {
                var thisVal = $(this).val();
                if (thisVal == 1) {
                    $("#divOpenDate").removeClass("hidden");
                    $("#divCloseDate").removeClass("hidden");
                } else {
                    $("#divOpenDate").addClass("hidden");
                    $("#divCloseDate").addClass("hidden");
                    FlashPay.Util.SetInputVal("limitCloseDate", "");
                    FlashPay.Util.SetInputVal("limitOpenDate", "");
                }
            })
        }

        //提交
        function saves() {
            if (!validator.form()) {
                return false;
            }
           
            var id_array = new Array();
            $('input[name="depositType"]:checked').each(function () {
                id_array.push($(this).val());
            });

            var viewModel = $("#formSearch").serialize();
            var datas = FlashPay.Util.parseQueryString2(viewModel);
            datas.companyID = $("select[name='companyID']").val();
            datas.companyName = $("input[name='companyName']").val();

            



            datas.depositType = id_array.join(',');

            var limitRepeat = $("input[name='limitRepeat']:checked").val();
            if (limitRepeat == 1) {
                datas.limitCloseDate = $("input[name='limitCloseDate']").val();
                datas.limitOpenDate = $("input[name='limitOpenDate']").val();

                if (FlashPay.Util.isNullOrEmptySpance(datas.limitOpenDate)) {
                    FlashPay.UI.Tip_short_warning("开启时间不能为空");
                    return;
                }
                if (FlashPay.Util.isNullOrEmptySpance(datas.limitCloseDate)) {
                    FlashPay.UI.Tip_short_warning("关闭时间不能为空");
                    return;
                }
                if (datas.limitCloseDate <= datas.limitOpenDate) {
                    FlashPay.UI.Tip_short_warning("关闭时间必须大于开启时间");
                    return;
                }
            }

            var url = "/SysConfig/AddOrEditInterface";
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                datas.type = 1;
            } else {
                datas.type = 0;
            }

            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: datas,
                success: function (data) {
                    cover.Remove();
                    if (!data.success) {
                        FlashPay.UI.Tip_short_warning(data.errorMessage == null ? "操作失败！" : data.errorMessage);
                        return;
                    } else {
                        FlashPay.UI.DialogOpener().Tip_success(data.successMessage == null ? "操作成功！" : data.successMessage);
                        FlashPay.UI.CloseDialog();
                        FlashPay.UI.DialogOpener().doSearch();
                    }
                }
            });
        }
    </script>
</body>
</html>